import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Dmo4 {

    public static void scan(File f,String key) throws IOException{
        if(!f.isDirectory()){
            return;
        }
        File[] files=f.listFiles();
        if(files==null||files.length==0){
            return;
        }
        for (File file:files){
            if(file.isFile()){
                 doDelete(file,key);
            }
            else{
                scan(file,key);
            }
        }
    }

    private static void doDelete(File file,String key) throws IOException {
        if(!file.getName().contains(key)){
            return;
        }
        else{
            Scanner scanner=new Scanner(System.in);
            System.out.println(file.getCanonicalPath()+"是否要删除Y/N");
            String choice=scanner.next();
            if(choice.equals("Y")||choice.equals("y")){
                file.delete();
            }
        }
    }

    public static void main(String[] args) throws IOException{
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要搜索的路径");
        String rootpath=scanner.next();
        File rootFlie=new File(rootpath);
        if(!rootFlie.isDirectory()){
            System.out.println("输入错误");
            return;
        }
        System.out.println("请输入要删除文件的关键字");
        Scanner scanner1=new Scanner(System.in);
        String key=scanner1.next();
        scan(rootFlie,key);
    }
}
